#!coding=utf-8
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import *
from django.views.decorators.csrf import csrf_exempt
from util.form_util import *

from enterprise_facilities.models import *
from enterprise_basic_information.models import *
from park.models import *

# 获取当前园区 和 企业列表
def isLogin(req):
	admin = req.session.get('admin', None)
	park = req.session.get('park', None)
	
	if not admin and not park:
		return False, False
	else:
		es = Enterprise.objects.filter(park = park)

		if len(es) > 0:
			return park, es
		else:
			return False, False

def productAreaList(request):
	p, es = isLogin(request)

	if not p and not es:
		return HttpResponse('当前用户信息错误')

	year = request.GET.get('y', None)
	month = request.GET.get('m', None)
	eid = request.GET.get('eid', None)
	ename = request.GET.get('ename', None)

	try:
		ps = []
		pds = ProductArea.objects.all()

		if year and month:
			pds = pds.filter(add_time__year=int(year), add_time__month=int(month))

		if eid or ename:
			if eid:
				pds = pds.filter(enterprise = int(eid))
				
			if ename:
				pds = pds.filter(enterprise__name__contains = ename)

			ps.extend(pds)
		else:
			for e in es:
				ps.extend(pds.filter(enterprise=e))

		paginator = Paginator(ps, 10)
		page = request.GET.get('page')
		ps = paginator.page(page)
	except PageNotAnInteger:
		ps = paginator.page(1)
	except EmptyPage:
		ps = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse("非法请求\n\n"+ e.message)
 	
	return render_to_response('park/facilities/production/productAreaList.html', {  'ps':ps, 'es':es, 'paginator':paginator })

def productAreaDetail(request, id):
    try:
        p = ProductArea.objects.get(id = id)

        ps = Production.objects.filter(product_area = p.id)

        paginator = Paginator(ps, 10)
        page = request.GET.get('page')
        ps = paginator.page(page)
    except PageNotAnInteger:
        ps = paginator.page(1)
    except EmptyPage:
        ps = paginator.page(paginator.num_pages)
    except Exception as e:
        return HttpResponse("非法请求\n\n"+ e.message)

    return render_to_response('park/facilities/production/productAreaDetail.html', { 'p':p, 'ps':ps }, context_instance=RequestContext(request))

def productionList(request):
	p, es = isLogin(request)

	if not p and not es:
		return HttpResponse('当前用户信息错误')

	year = request.GET.get('y', None)
	month = request.GET.get('m', None)
	eid = request.GET.get('eid', None)
	ename = request.GET.get('ename', None)

	try:
		ps = []
		pds = ProductArea.objects.all()

		if year and month:
			pds = pds.filter(add_time__year=int(year), add_time__month=int(month))

		if eid or ename:
			if eid:
				pds = pds.filter(enterprise = int(eid))
				
			if ename:
				pds = pds.filter(enterprise__name__contains = ename)

			ps.extend(pds)
		else:
			for e in es:
				ps.extend(pds.filter(enterprise=e))

		paginator = Paginator(ps, 10)
		page = request.GET.get('page')
		ps = paginator.page(page)
	except PageNotAnInteger:
		ps = paginator.page(1)
	except EmptyPage:
		ps = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse("非法请求\n\n"+ e.message)
 	
	return render_to_response('park/facilities/production/production_list.html', {  'ps':ps, 'es':es, 'paginator':paginator })

def productionDetail(request, id):
    p = Production.objects.get(id = id)
    return render_to_response('park/facilities/production/production_detail.html', {'p':p }, context_instance=RequestContext(request))


def storageAreaList(request):
	p, es = isLogin(request)

	if not p and not es:
		return HttpResponse('当前用户信息错误')

	year = request.GET.get('y', None)
	month = request.GET.get('m', None)
	eid = request.GET.get('eid', None)
	ename = request.GET.get('ename', None)

	try:
		sas = []
		sass = StorageArea.objects.all()

		if year and month:
			sass = sass.filter(add_time__year=int(year), add_time__month=int(month))

		if eid or ename:
			if eid:
				sass = sass.filter(enterprise = int(eid))
				
			if ename:
				sass = sass.filter(enterprise__name__contains = ename)

			sas.extend(sass)
		else:
			for e in es:
				sas.extend(sass.filter(enterprise=e))

		paginator = Paginator(sas, 10)
		page = request.GET.get('page')
		sas = paginator.page(page)
	except PageNotAnInteger:
		sas = paginator.page(1)
	except EmptyPage:
		sas = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse("非法请求\n\n"+ e.message)
 	
	return render_to_response('park/facilities/storage/storageAreaList.html', {  'sas':sas, 'es':es, 'paginator':paginator })

def storageAreaDetail(request, id):
    try:
        sa = StorageArea.objects.get(id = id)

        sts = StorageTank.objects.filter(storage_area = sa.id)

        paginator = Paginator(sts, 10)
        page = request.GET.get('page')
        sts = paginator.page(page)
    except PageNotAnInteger:
        sts = paginator.page(1)
    except EmptyPage:
        sts = paginator.page(paginator.num_pages)
    except Exception as e:
        return HttpResponse("非法请求\n\n"+ e.message)

    return render_to_response('park/facilities/storage/storageAreaDetail.html', { 'sa':sa, 'sts':sts }, context_instance=RequestContext(request))


def storageList(request):
	p, es = isLogin(request)

	if not p and not es:
		return HttpResponse('当前用户信息错误')

	year = request.GET.get('y', None)
	month = request.GET.get('m', None)
	eid = request.GET.get('eid', None)
	ename = request.GET.get('ename', None)

	try:
		sts = []
		stss = StorageTank.objects.all()

		if year and month:
			stss = stss.filter(add_time__year=int(year), add_time__month=int(month))

		if eid or ename:
			if eid:
				stss = stss.filter(enterprise = int(eid))
				
			if ename:
				stss = stss.filter(enterprise__name__contains = ename)

			sts.extend(stss)
		else:
			for e in es:
				sts.extend(stss.filter(enterprise=e))

		paginator = Paginator(sts, 10)
		page = request.GET.get('page')
		sts = paginator.page(page)
	except PageNotAnInteger:
		sts = paginator.page(1)
	except EmptyPage:
		sts = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse("非法请求\n\n"+ e.message)
	
	return render_to_response('park/facilities/storage/storage_list.html', { 'sts':sts, 'es':es, 'paginator':paginator }, context_instance=RequestContext(request))

def storageDetail(request, id):
	st = StorageTank.objects.get(id = id)
	return render_to_response('park/facilities/storage/storage_detail.html', { 'st':st }, context_instance=RequestContext(request))
